package com.hsmw.api.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.hsmw.api.exception.ApiException;
import com.hsmw.api.mapper.HsmwBizUserMapper;
import com.hsmw.api.mapper.HsmwPmUserMapper;
import com.hsmw.common.base.BaseService;
import com.htn.common.core.base.DataResponse;
import com.htn.common.core.constant.CommonConstants;
import com.hsmw.domain.HsmwBizUser;
import com.hsmw.domain.HsmwPmUser;
import com.hsmw.domain.HsmwUser;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.Objects;
import java.util.Optional;

import static com.htn.common.core.constant.CommonConstants.ResponseStatus.SUCCESS;

@Service
@Transactional(isolation = Isolation.REPEATABLE_READ, propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
public class HsmwPmUserService extends BaseService<HsmwPmUserMapper, HsmwPmUser> {

    @Autowired
    private HsmwPmUserMapper hsmwPmUserMapper;


    public HsmwPmUser findByUsername(String username) {
        QueryWrapper<HsmwPmUser> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(HsmwPmUser::getUsername, username);
        return hsmwPmUserMapper.selectOne(queryWrapper);
    }


    /**
     * 新增或修改 逻辑删除
     *
     * @return
     */
    public DataResponse saveOrUpdateHsmwUser(HsmwPmUser hsmwUser) {
        if (StringUtils.isNotBlank(hsmwUser.getId())) {
            LambdaQueryWrapper<HsmwPmUser> lambdaQueryWrapper = new LambdaQueryWrapper();
            lambdaQueryWrapper.eq(HsmwPmUser::getUsername, hsmwUser.getUsername());
            HsmwPmUser existHsmwUser = hsmwPmUserMapper.selectOne(lambdaQueryWrapper);
            if (existHsmwUser != null && !Objects.equals(existHsmwUser.getId(), hsmwUser.getId())) {
                throw new ApiException(CommonConstants.ResponseStatus.USER_IS_EXIST);
            } else {
                hsmwPmUserMapper.updateById(hsmwUser);
            }
        } else {
            LambdaQueryWrapper<HsmwPmUser> lambdaQueryWrapper = new LambdaQueryWrapper();
            lambdaQueryWrapper.eq(HsmwPmUser::getUsername, hsmwUser.getUsername());
            HsmwPmUser existHsmwUser = hsmwPmUserMapper.selectOne(lambdaQueryWrapper);
            if (existHsmwUser == null) {
                hsmwUser.setEnabled(true);
                hsmwUser.setFlag(false);
                //hsmwUser.setPassword(bCryptPasswordEncoder.encode(Optional.ofNullable(hsmwUser.getPassword()).orElse("")));
                hsmwPmUserMapper.insert(hsmwUser);
            } else {
                throw new ApiException(CommonConstants.ResponseStatus.USER_IS_EXIST);
            }
        }
        return new DataResponse(SUCCESS);
    }

    public void removeItemInfoByItemId(HashMap param) {
        hsmwPmUserMapper.removeItemInfoByItemId(param);
    }

}
